//===============================================================================
// Microsoft patterns & practices Enterprise Library Contribution
// Mapping Application Block
//===============================================================================

using System;
using System.IO;
using System.Runtime.Serialization.Formatters.Binary;
using Microsoft.Practices.EnterpriseLibrary.Configuration.Design;

namespace EntLibContrib.Mapping.Configuration.Design
{
	/// <summary>
	/// Copy mapper node command
	/// </summary>
	/// <remarks>This command will create a copy of the selected mapper complete
	/// with a copy of all the maps</remarks>
	public class CopyMapperNodeCommand : ConfigurationNodeCommand
	{
		#region Construction
		/// <summary>
		/// Initializes a new instance of the <see cref="CopyMapperNodeCommand"/> class.
		/// </summary>
		/// <param name="serviceProvider">The service provider.</param>
		public CopyMapperNodeCommand(IServiceProvider serviceProvider)
			: base(serviceProvider)
		{ }
		#endregion

		#region Public Methods
		/// <summary>
		/// When overridden by a class, executes the core logic of the command.
		/// </summary>
		/// <param name="node">The node to execute the command upon.</param>
		protected override void ExecuteCore(ConfigurationNode node)
		{
			// make sure this command only executes on mapper nodes
			MapperNode sourceNode = node as MapperNode;
			if (sourceNode != null)
			{
				CopyMapper(sourceNode);
			}
		}
		#endregion

		#region Private Methods
		private static void CopyMapper(MapperBaseNode sourceNode)
		{
			// create a clone copy of the mapper node
			sourceNode.Parent.AddNode(sourceNode.Clone());
		}
		#endregion
	}
}
